package com.test.webflux.config;

import com.test.webflux.entity.Product;
import com.test.webflux.entity.ResponseResult;
import com.test.webflux.service.ProductService;
import com.test.webflux.service.UserService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.*;
import reactor.core.publisher.Mono;


/**
 * 动态路由
 * @Description
 * @Author lingmin
 * @Date 2025/4/14 16:49
 **/
//@Configuration
public class DynamicRouteConfig {



    @Bean
    public RouterFunction<ServerResponse> dynamicRoutes(PathConfig pathConfig , UserService userService , ProductService productService) {
        RouterFunctions.Builder builder = RouterFunctions.route();
        for (PathConfig.PathInfo pathInfo : pathConfig.getPathInfos()) {
            RouterFunction<ServerResponse> routeTmp = null;
            if("GET".equals(pathInfo.getMethodName())){
                routeTmp = RouterFunctions.route().GET(pathInfo.getPath(), request
                                -> handleDynamicPath(request , pathInfo , userService , productService)).build();
            }
            if("POST".equals(pathInfo.getMethodName())){
                routeTmp = RouterFunctions.route()
                        .POST(pathInfo.getPath(), request
                                -> handleDynamicPath(request , pathInfo , userService , productService)).build();
            }
            builder.add(routeTmp);
        }
        return builder.build();
    }


    /**
     * 动态路由处理
     * @param request
     * @param pathInfo
     * @param userService
     * @param productService
     * @return
     */
    private Mono<ServerResponse> handleDynamicPath(ServerRequest request , PathConfig.PathInfo pathInfo
            , UserService userService , ProductService productService) {

        if("/user/title".equals(pathInfo.getPath())){
            return ServerResponse.ok().body(userService.getTitle() , String.class);
        }
        if("/user/list".equals(pathInfo.getPath())){
            return ServerResponse.ok().body(userService.list() , ResponseResult.class);
        }

        if("/product/title".equals(pathInfo.getPath())){
            return ServerResponse.ok().body(productService.getTitle() , String.class);
        }
        //
        if("/product/save".equals(pathInfo.getPath())){
            return ServerResponse.ok().body(productService.save(request.bodyToMono(Product.class)) , ResponseResult.class);
        }
        if("/product/save2".equals(pathInfo.getPath())){
            return productService.save2(request);
        }
        if("/product/subscribe".equals(pathInfo.getPath())){
            return ServerResponse.ok().build(productService.subscribe(request.bodyToMono(Product.class)));
        }
        if("/product/getProductById/{id}".equals(pathInfo.getPath())){
            return ServerResponse.ok().body(productService.getProductById(request) , ResponseResult.class);
        }
        if("/product/list".equals(pathInfo.getPath())){
            return request.bodyToMono(Product.class).flatMap(product -> {
                return ServerResponse.ok().body(productService.list(product) , ResponseResult.class);
            });
        }
        return ServerResponse.ok().bodyValue("未匹配到dynamic path: " + pathInfo.getPath());
    }
}
